<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<link href="<%=request.getContextPath() %>/css/Style.css" type="text/css" rel="stylesheet">
<link href="<%=request.getContextPath() %>/css/facebox.css" type="text/css" rel="stylesheet">
<script type="text/javascript" src="<%=request.getContextPath() %>/js/jquery-1.3.2.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/js/facebox.js"></script>
<script type="text/javascript">
	var $j = jQuery.noConflict();
	function checkAll(obj) {
		var news = document.getElementsByName("msgs");
		if (obj.checked) {
			for ( var i = 0; i < news.length; i++) {
				news[i].checked = true;
			}
		}else{
			for ( var i = 0; i < news.length; i++) {
				news[i].checked = false;
			}
		}
	}
	function promptDialog(callback, args) {
		if (confirm("是否执行该操作?")) {
			if (args != null) {
				callback(args);
			} else {
				callback();
			}
		}
	}
	
	function deleteItem() {
		var news = document.getElementsByName("msgs");
		var deleteItem = "";
		for ( var i = 0; i < news.length; i++) {
			if(news[i].checked){
				deleteItem+=news[i].value+",";
			}
		}
		if(deleteItem==""){
				alert("至少选择一个才能进行操作！");
				return false;
			}
		var path='<%=request.getContextPath()%>';
		deleteItem = deleteItem.substring(0,deleteItem.length-1);
		$j.ajax({
			type: "GET",
            url: path + "/deleteRemoteCalls.htm",
            data:{
				ids:deleteItem
            },
            dataType: "text",
            success: function(result){
            	 if(result=="success"){
                 	 alert("操作成功！");
                	 //window.location.href=href;
                 	document.getElementById("n_form").submit();
                 }else{
              	   alert("操作失败！");
                     }
            },
            error: function(){
              alert("数据库繁忙，请稍后再试！");
            }
		});
	}
	function goPage(page){
			document.getElementById("currentPage").value=page;
			document.getElementById("n_form").submit();
		}
	
	function addRemoteCall() {
		//facebox会复制一次，所以去下标为1
		var _callNumber = document.getElementsByName("callNumber")[1].value;
		if (_callNumber == null || _callNumber.length < 1) {
			alert("请输入呼叫号码！");
			return;
		}
		var _callDuration = document.getElementsByName("callDuration")[1].value;
		if (_callDuration == null || !_callDuration.match(/^(\d{1,6})$/) || _callDuration<1) {
			alert("请输入正确的呼叫时长！");
			return;
		}
		var _callTime = document.getElementsByName("callTime")[1].value;
		//var dateRegex = "/^(\d{1,4})\-(\d{1,2})\-(\d{1,2}) (\d{1,2}):(\d{1,2})$/";
		if (_callTime != null && _callTime.length > 0
				&& !_callTime.match(/^(\d{1,4})\-(\d{1,2})\-(\d{1,2}) (\d{1,2}):(\d{1,2})$/)) {
			alert("请输入正确的时间格式，如： 2013-8-30 12:30");
			return;
		}
		var _deleteAfter = document.getElementsByName("deleteAfter")[1].value;
		var _phone = document.getElementById("phone").value;
		var path='<%=request.getContextPath()%>';
		var _submitBtn = document.getElementsByName("submitBtn")[1];
		_submitBtn.value="提交中...";
		_submitBtn.disabled="disabled";
		var _loading = document.getElementsByName("loading")[1];
		_loading.style.display="";
		$j.ajax({
			type: "GET",
            url: path + "/addRemoteCall.htm",
            data:{
				phone:_phone,
				deleteAfterCall:_deleteAfter,
				callDuration:_callDuration,
				callTime:_callTime,
				callNumber:_callNumber
            },
            dataType: "text",
            success: function(result){
            	 if(result=="success"){
                	 //刷新页面
                 	document.getElementById("n_form").submit();
                 }else{
              	  	 alert("操作失败！");
              	  	 _submitBtn.value="提交";
              	  	 _submitBtn.disabled="";
              	     _loading.style.display="none";
                 }
            },
            error: function(){
              alert("服务器繁忙，请稍后再试！");
              _submitBtn.value="提交";
              _submitBtn.disabled="";
          	  _loading.style.display="none";
            }
		});
	}

	$j(document).ready(function() {
		var path = '<%=request.getContextPath()%>';
		  $j('a[rel*=facebox]').facebox({
			  loadingImage : path+'/images/loading.gif',
		      closeImage  : path+'/images/closelabel.png'
		  }); 
		})
		
</script>
</head>
<body>
<table border="0" cellspacing="0" cellpadding="0" class="thead" width="100%">
	<tr><td style="padding:3px;">&nbsp;</td></tr>
</table>
<form action="<%=request.getContextPath() %>/remoteCallList.htm" method="post" id="n_form">
<table cellspacing="0" cellpadding="0" bordercolor="#003366" border="1" width="100%" class="Table1">
	<tbody>
		<tr height="30px">
			<td colspan="6" >
				<span class="top_c">
					<input type="button" value="删除" onclick="promptDialog(deleteItem,null)" class="btn"/>
					<a rel="facebox" href="#add_remotecall"> <input type="button" value="添加" class="btn" style="margin-left: 5px;"/></a>
				</span>
			</td>
		</tr>
		<tr>
			<td height="25" align="center" ><input type="checkbox" onclick="checkAll(this)" /></td>
			<td height="25" align="center" >呼叫号码</td>
			<td height="25" align="center" >呼叫时长</td>
			<td height="25" align="center" >呼叫时间</td>
			<td height="25" align="center" >删除通话记录</td>
			<td height="25" align="center" >状态</td>
		</tr>
		<s:iterator value="calls.datas" id="item" status="index">
		<tr>
			<td height="25" align="center"><input type="checkbox" name="msgs" value="<s:property value="#item.id"/>"/></td>
			<td height="25" align="center">
				<s:property value="#item.callNumber"/>
			</td>
			<td height="25" align="center">
				<s:property value="#item.duration"/>秒
			</td>
			<td height="25" align="center"><s:date name="#item.callAfter" nice="false" format="yyyy-MM-dd HH:mm"/></td>
			<td height="25" align="center">
				<s:if test="#item.deleteCallLog">
						是
					</s:if>
					<s:else>
						否
					</s:else>
			</td>
			<td height="25" align="center">
					<s:if test="#item.excuted">
						已执行
					</s:if>
					<s:else>
						<font color="red">未执行</font>
					</s:else>
			</td>		
		</tr>
		</s:iterator>
		<tr>
			<td colspan="6" height="30px" align="center">
			<span style="padding-right:10px">[第<b>${calls.currentPage}</b>页|共<b>${calls.pageCount}</b>页]
					<s:if test="calls.currentPage<2"><span class="disable" >&lt;&lt;上一页</span></s:if><s:else><a class="enable" href="javascript:goPage(${calls.currentPage}-1)">&lt;&lt;上一页</a></s:else>
					<s:if test="calls.currentPage>=calls.pageCount"><span class="disable">下一页&gt;&gt;</span></s:if><s:else><a class="enable" href="javascript:goPage(${calls.currentPage+1})">下一页&gt;&gt;</a></s:else>
			</span>
			</td>
		</tr>
	</tbody>
</table>
	<input type="hidden" value="${calls.currentPage}" name="cPage" id="currentPage" />
	<input type="hidden" value="${phone}" name="phone" id="phone" />
</form>
	<div id="add_remotecall" style="display: none;">
		<table width="500px"  cellspacing="0" cellpadding="0" bordercolor="#003366" border="1" class="Table1">
			<tbody>
				<tr >
					<td height="25" width="20%" align="right">
						呼叫电话:
					</td >
					<td height="25" width="30%">
						<input  type="text" name="callNumber" class="account" width="100%">
					</td>
					<td height="25" width="50%">
						<font color="red">*</font>呼叫电话号码
					</td>
				</tr>
				
				<tr >
					<td height="25" width="20%" align="right">
						呼叫时长:
					</td >
					<td height="25" width="30%">
						<input  type="text" name="callDuration" class="account" width="100%">
					</td>
					<td height="25" width="50%">
						<font color="red">*</font>呼叫时长（秒）
					</td>
				</tr>
				
				<tr >
					<td height="25" width="20%" align="right">
						删除记录:
					</td >
					<td height="25" width="30%">
						<select name="deleteAfter">
							<option value="1">删除</option>
							<option value="0">不删除</option>
						</select>
					</td>
					<td height="25" width="50%">
						<font color="red">*</font>拨打后从通话记录中删除
					</td>
				</tr>
				
				<tr >
					<td height="25" width="20%" align="right">
						呼叫时间:
					</td >
					<td height="25" width="30%">
						<input  type="text" name="callTime" class="account" width="100%">
					</td>
					<td height="25" width="50%">
						（非必须参数）表示在设置的时间之后才会拨打电话,默认当前时间；<br/>示例   2013-8-30 12:30
					</td>
				</tr>
				<tr >
					<td height="25" align="center"  colspan="3">
						<input type="button" value="添加" class="btn" name="submitBtn"  onclick="addRemoteCall();">
						<img alt="" src="<%=request.getContextPath()%>/images/loading.gif" style="max-height: 20px;max-width: 20px;display:none;" name="loading">
					</td>
				</tr>

			</tbody>
		</table>
	</div>
</body>
</html>